package cn.itcast.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.itcast.entity.Result;
import cn.itcast.mapper.ReportMapper;
import cn.itcast.service.ReportService;
import cn.itcast.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ObjectUtil;

import java.sql.Date;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportMapper reportMapper;
    @Override
    public MemberReportVo selectCountGroupByMonth() {
        List<MemberCountVo> memberCountVos = reportMapper.selectCountGroupByMonth();
        List<Object> months =CollUtil.getFieldValues(memberCountVos, "months");
        List<Object> memberCounts = CollUtil.getFieldValues(memberCountVos, "memberCount");
        MemberReportVo memberReportVo=new MemberReportVo();
        memberReportVo.setMonths(Convert.toList(String.class, months));
        memberReportVo.setMemberCount(Convert.toList(String.class, memberCounts));
        return memberReportVo;
    }

    @Override
    public SetMealReportVo selectSetMealCount() {
        List<SetMealVo> setMealVos = reportMapper.selectSetMealCount();
        List<Object> setmealNames =CollUtil.getFieldValues(setMealVos, "setmealNames");
//        List<Object> setmealCount = CollUtil.getFieldValues(setMealVos, "setmealCount");
        SetMealReportVo setMealReportVo=new SetMealReportVo();
        List<EchartsVo> echartsVos=new ArrayList<>();
        for (SetMealVo setMealVo : setMealVos) {
            EchartsVo echartsVo=new EchartsVo();
            echartsVo.setName(setMealVo.getSetmealNames());
            echartsVo.setValue(setMealVo.getSetmealCount());
            echartsVos.add(echartsVo);
        }
        setMealReportVo.setSetmealCount(echartsVos);
        setMealReportVo.setSetmealNames(Convert.toList(String.class,setmealNames));
        return setMealReportVo;
    }

    @Override
    public IncomeReportVo selectIncomeGroupByMonth() {
        List<IncomeVo> incomeVos = reportMapper.selectIncomeGroupByMonth();
        List<Object> months =CollUtil.getFieldValues(incomeVos, "months");
        List<Object> incomes = CollUtil.getFieldValues(incomeVos, "incomes");
        IncomeReportVo incomeReportVo=new IncomeReportVo();
        incomeReportVo.setMonths(Convert.toList(String.class, months));
        incomeReportVo.setIncomes(Convert.toList(String.class, incomes));
        return incomeReportVo;
    }

    @Override
    public OrderReportVo selectOrderAndPayCount() {
        List<OrderVo> orderVos = reportMapper.selectOrderAndPayCount();

        List<Object> months =CollUtil.getFieldValues(orderVos, "months");
        List<Object> orderCount = CollUtil.getFieldValues(orderVos, "orderCount");
        List<Object> payCount = CollUtil.getFieldValues(orderVos, "payCount");
        OrderReportVo orderReportVo=new OrderReportVo();
        orderReportVo.setMonths(Convert.toList(String.class, months));
        orderReportVo.setOrderCounts(Convert.toList(String.class, orderCount));
        orderReportVo.setPayCounts(Convert.toList(String.class, payCount));
        return orderReportVo;

    }

    @Override
    public BusinessReportVo selectBusiness() {
        BusinessReportVo businessReportVo=new BusinessReportVo();

        businessReportVo.setReportDate(new Date(System.currentTimeMillis()));

        businessReportVo.setThisMonthNewMember(reportMapper.selectThisMonthNewMember());
        businessReportVo.setTodayNewMember(reportMapper.selectTodayNewMember());
        businessReportVo.setThisWeekNewMember(reportMapper.selectThisWeekNewMember());
        businessReportVo.setTotalMember(reportMapper.selectTotalMember());

        businessReportVo.setTodayOrderNumber(reportMapper.selectTodayOrderNumber());
        businessReportVo.setTodayVisitsNumber(reportMapper.selectTodayVisitsNumber());

        businessReportVo.setThisWeekOrderNumber(reportMapper.selectThisWeekOrderNumber());
        businessReportVo.setThisWeekVisitsNumber(reportMapper.selectThisWeekVisitsNumber());

        businessReportVo.setThisMonthOrderNumber(reportMapper.selectThisMonthOrderNumber());
        businessReportVo.setThisMonthVisitsNumber(reportMapper.selectThisMonthVisitsNumber());

        List<HotSetmeal> hotSetmeals = reportMapper.selectHotSetmealAndOrderCount();
        businessReportVo.setHotSetmeal(hotSetmeals);
        return businessReportVo;
    }
}
